﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using ProjetoClasse.Model.Entidade;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

namespace ProjetoClasse.Util
{
    public static class FacebookUtil
    {
        public const string APPID = "204094279626024";
        public const string APPSECRET = "f62a2b109c779f32d01b801584dbdbda";

        public static void inviteFriends(string userToken, List<Int64> listaUsuarios)
        {
            try
            {
                WebClient wc = new WebClient();
                wc.Encoding = Encoding.UTF8;
                foreach (Int64 f in listaUsuarios)
                {
                    wc.UploadString(@"https://graph.facebook.com/" + f + "/feed", null, "access_token=" + userToken + "&message=Venha participar do Juntae! Sua rede social de eventos! Acesse já www.juntae.com.br");
                }
            }
            catch (System.Net.WebException ex)
            {
                StreamReader readStream = new StreamReader(ex.Response.GetResponseStream());
            }
        }

        public static string getUserToken(string code)
        {
            return new WebRequestUtil().getResponse(new Uri(@"https://graph.facebook.com/oauth/access_token?redirect_uri=http://localhost:49601/Users/WizardCadastro.aspx&client_id=" + FacebookUtil.APPID + "&code=" + code + "&client_secret=" + FacebookUtil.APPSECRET), "GET");
        }

        public static List<FacebookUsersDTO> getAllUsers(string userToken)
        {
            string json = new WebRequestUtil().getResponse(new Uri(@"https://graph.facebook.com/me/friends?fields=id,name,picture&access_token=" + userToken), "GET");

            Dictionary<string, object> teste = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
            object o = teste["data"];
            return JsonConvert.DeserializeObject<List<FacebookUsersDTO>>(o.ToString());
        }
    }
}